﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DnD.UI.Panels
{
    public class Stacker
    {
        public static void SimpleStack(Control parent)
        {

        }
        public static void ReStack(Control parent, int minWidth, int maxHorizontalRepeats)
        {
            int availableWidth = parent.Width;
            int repeats = availableWidth / minWidth;
            if (repeats < 1)
                throw new Exception();
            if (repeats > maxHorizontalRepeats)
                repeats = maxHorizontalRepeats;

            int objectWidth = availableWidth / repeats;
            int offsetTop = 0;
            for (int y = 0; y < parent.Controls.Count; y++)
            {
                int lineHeight = 0;
                for (int x = 0; x < repeats && y < parent.Controls.Count; x++, y++)
                {
                    Control c = parent.Controls[y];
                    c.Width = objectWidth;
                    c.Top = offsetTop;
                    c.Left = x * objectWidth;
                    if (c.Height > lineHeight)
                        lineHeight = c.Height;
                }
                offsetTop += lineHeight;
                y--;
            }
            parent.Height = offsetTop;    


        }
    }
}
